package phapp.action.custom;

import java.io.*;
import java.util.*;

import javax.servlet.http.*;

import net.sf.json.*;

import javacommon.util.*;
import javacommon.jquery.*;
import static javacommon.util.Struts2JsonHelper.*;

import com.opensymphony.xwork2.*;
import cn.org.rapid_framework.page.*;

import phapp.model.custom.CustomFavorite;

import javacommon.base.*;

import phapp.model.custom.*;
import phapp.dao.custom.*;
import phapp.service.custom.impl.*;
import phapp.service.custom.*;

/**
 * Title: t_custom_favorite Description: 接收前面界面请求,通过json数据格式交互 Action
 * Description: 页面通过Ajax调用Action层,返回JSONO数据 Copyright: Copyright (c) 2012-10
 * Company:
 * 
 * @author you name
 * @version 1.0
 */

public class CustomFavoriteAction extends BaseStruts2Action
{

   private CustomFavoriteManager customfavoriteManager;

   public void setCustomFavoriteManager(CustomFavoriteManager manager)
   {
      this.customfavoriteManager = manager;
   }

   /**
    * jqGrid使用列表 分页查询
    * 
    * @throws IOException
    * @author
    */
   public void list() throws IOException
   {
      PageRequest<Map> pr = JqueryPageHelper.createPageRequestForJquery(getRequest());
      Page page = customfavoriteManager.findByPageRequest(pr);

      List<CustomFavorite> CustomFavoritelist = (List) page.getResult();
      ListRange<CustomFavorite> resultList = new ListRange<CustomFavorite>();
      resultList.setDataRows(CustomFavoritelist);
      resultList.setRecords(page.getTotalCount());
      resultList.setRows(page.getPageSize());
      if (page.getTotalCount() % page.getPageSize() > 0)
      {
         resultList.setTotal(page.getTotalCount() / page.getPageSize() + 1);
      }
      else
      {
         resultList.setTotal(page.getTotalCount() / page.getPageSize());
      }
      resultList.setPage(pr.getPageNumber());
      outJson(resultList);
   }

   /**
    * CustomFavorite添加
    * 
    * @author
    * @throws IOException
    */
   public void save() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String customfavoriteJson = request.getParameter("customfavoriteJson");
         if (null != customfavoriteJson && !"".equals(customfavoriteJson))
         {
            JSONObject jsonobject = JSONObject.fromObject(customfavoriteJson);
            CustomFavorite customfavoriteobj = (CustomFavorite) JSONObject.toBean(jsonobject, CustomFavorite.class);
            customfavoriteManager.save(customfavoriteobj);
            result.put("success", true);
            result.put("msg", "添加成功!");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "添加失败 数据错误!");
         }
      }
      catch (Exception e)
      {
         result.put("success", false);
         result.put("msg", "添加失败!");
         e.printStackTrace();
      }
      outJson(result);
   }

   /**
    * CustomFavorite修改
    * 
    * @throws IOException
    * @author
    */
   public void update() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String customfavoriteJson = request.getParameter("customfavoriteJson");
         if (null != customfavoriteJson && !"".equals(customfavoriteJson))
         {
            JSONObject jsonobject = JSONObject.fromObject(customfavoriteJson);
            CustomFavorite customfavoriteobj = (CustomFavorite) JSONObject.toBean(jsonobject, CustomFavorite.class);

            /** 设置主键值 */
            // customfavoriteobj.setId(jsonobject.get("id").toString());
            customfavoriteobj.setId(new Integer(jsonobject.get("id").toString()));

            customfavoriteManager.update(customfavoriteobj);
            result.put("success", true);
            result.put("msg", "修改成功!");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "修改失败 数据错误!");
         }
      }
      catch (Exception e)
      {
         result.put("success", false);
         result.put("msg", "修改失败!");
         e.printStackTrace();
      }
      outJson(result);
   }

   /**
    * CustomFavorite删除
    * 
    * @throws IOException
    * @author
    */
   public void delete() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String ids = request.getParameter("ids");
         if (null != ids && !"".equals(ids))
         {
            CustomFavorite customfavorite = new CustomFavorite();
            customfavorite.setIdList(StringTool.stringToArray(ids));
            customfavoriteManager.removeByIdArry(customfavorite);
            result.put("success", true);
            result.put("msg", "删除成功");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "删除失败 数据错误!");
         }
      }
      catch (Exception e)
      {
         result.put("success", false);
         result.put("msg", "删除失败!");
         e.printStackTrace();
      }
      outJson(result);
   }
   
}
